package refactor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.bean.DeviceBean;
import common.bean.EventLogBean;
import common.standard.table.AllLogFieldEnum;
import common.standard.table.PageEnum;
import common.util.ConditionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import refactor.entity.AllLog;
import refactor.mapper.AllLogMapper;
import refactor.service.IAllLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import static org.apache.commons.lang.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author 杨健
 * @since 2019-08-19
 */
@Service
public class AllLogServiceImpl extends ServiceImpl<AllLogMapper, AllLog> implements IAllLogService {

    private AllLogMapper allLogMapper;

    @Autowired
    public void setAllLogMapper(AllLogMapper allLogMapper) {
        this.allLogMapper = allLogMapper;
    }


    /**
     * 根据条件分页显示事件日志
     *
     * @param info     条件
     * @param regionId 区域id
     * @param pageNum  当前页码
     * @return 返回分页包装对象
     */
    @Override
    public IPage<AllLog> pageList(EventLogBean info, Integer regionId, Integer pageNum) throws ParseException {

		QueryWrapper<AllLog> allLogQueryWrapper = new QueryWrapper<>();
		allLogQueryWrapper.eq(AllLogFieldEnum.REGIONID.getFieldName(), regionId);

		DeviceBean deviceBean = new DeviceBean(info.getJg(), info.getTdd(), info.getJfs(), info.getJgs(),info.getMenjinhezi(),info.getDianbiao(),info.getShuibiao(),info.getYangan());

		// 设备条件
		ConditionUtil.addDeviceCondition(allLogQueryWrapper, deviceBean);

		// 未解决条件
        if (isNotBlank(info.getJj())) {
        	allLogQueryWrapper.eq(AllLogFieldEnum.RES.getFieldName(), "0");
		}
		//紧急事件
		if (isNotBlank(info.getEmergency())) {
			allLogQueryWrapper.eq(AllLogFieldEnum.TYPEID.getFieldName(), 18);
		}
		//运行时事件
		if (isNotBlank(info.getRuntime())) {
			allLogQueryWrapper.eq(AllLogFieldEnum.TYPEID.getFieldName(), 7);
		}

		// 将查询条件转换为对应的值
		Integer wordOrderCondition = isBlank(info.getT1()) ? null : 1;
		Integer historyCondition = isBlank(info.getT2()) ? null : 2;
		Integer exceptionCondition = isBlank(info.getT3()) ? null : 3;
		Integer temporaryLockApplyCondition = isBlank(info.getT4()) ? null : 4;
		Integer agreeTemporaryLockApplyCondition = isBlank(info.getT5()) ? null : 5;
		Integer unboundCondition = isBlank(info.getT6()) ? null : 6;

		// 查询条件
		if (wordOrderCondition != null
				|| historyCondition != null
				|| exceptionCondition != null
				|| temporaryLockApplyCondition != null
				|| agreeTemporaryLockApplyCondition != null
				|| unboundCondition != null) {
			allLogQueryWrapper.nested(i -> {
				addQueryCondition(wordOrderCondition, historyCondition, exceptionCondition, i);
				i.or();

				addQueryCondition(temporaryLockApplyCondition, agreeTemporaryLockApplyCondition, unboundCondition, i);
				return i;
			});
		}

		//进行时间类型转换
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		long startTime = (info.getF() == null || "".equals(info.getF())) ? 0 : format.parse(info.getF()).getTime();
		long endTime = (info.getT() == null || "".equals(info.getT())) ? System.currentTimeMillis() : format.parse(info.getT()).getTime();

		// 时间条件
		allLogQueryWrapper.ge(AllLogFieldEnum.STARTTIME.getFieldName(), startTime)
				.le(AllLogFieldEnum.STARTTIME.getFieldName(), endTime);

		Page<AllLog> alllogPage = new Page<>(pageNum, PageEnum.PAGE_SIZE.getValue());

        return allLogMapper.selectPage(alllogPage, allLogQueryWrapper);
    }

	/**
	 * 添加查询条件
	 *
	 * @param wordOrderCondition 工单条件
	 * @param historyCondition 历史条件
	 * @param exceptionCondition 异常条件
	 * @param i 返回QueryWrapper
	 */
	private void addQueryCondition(Integer wordOrderCondition, Integer historyCondition, Integer exceptionCondition, QueryWrapper<AllLog> i) {
		if (wordOrderCondition != null) {
			i.eq(AllLogFieldEnum.TYPEID.getFieldName(), wordOrderCondition);
		}
		i.or();

		if (historyCondition != null) {
			i.eq(AllLogFieldEnum.TYPEID.getFieldName(), historyCondition);
		}
		i.or();

		if (exceptionCondition != null) {
			i.eq(AllLogFieldEnum.TYPEID.getFieldName(), exceptionCondition);
		}
	}
}
